package com.szc.demo01.controller;

import com.szc.demo01.pojo.Emp;
import com.szc.demo01.pojo.Office;
import com.szc.demo01.pojo.User;
import com.szc.demo01.service.EmpService;
import com.szc.demo01.service.OfficeService;
import com.szc.demo01.controller.base.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * 负责管理office表的操作
 * @Author: WeiXin
 * @Date: 2021/01/14 8:01:44
 */
@Controller
public class OfficeController extends BaseController{
    @Resource
    private OfficeService officeService;
    @Resource
    private EmpService empService;
    /**
     * 查看未报销商品
     */
    @RequestMapping("/not_bx")
    public String notBx(Model model){
        long id = getUserId();

        List<Office> list;
        if (userIdHasPerm(5)){
            list =this.officeService.listBx(0);
        }else {
            list =this.officeService.selfBx(id,0);
        }
        model.addAttribute("list",list);
        return "office/off_weibaoxiaolist";
    }

    /**
     * 查看已报销商品
     */
    @RequestMapping("/is_bx")
    public String isBx(Model model){
        long id = getUserId();
        List<Office> list;
        if (userIdHasPerm(5)){
            list =this.officeService.listBx(1);
        }else {
            list =this.officeService.selfBx(id,1);
        }
        model.addAttribute("list",list);
        return "office/off_yibaoxiaolist";
    }

    /**
     *查看已回收商品
     */
    @RequestMapping("/is_huishou")
    public String isHuishou(Model model){
        long id = getUserId();

        List<Office> list;
        if (userIdHasPerm(5)){
            list =this.officeService.listBx(2);
        }else {
            list =this.officeService.selfBx(id,2);
        }
        model.addAttribute("list",list);
        return "office/off_yihuishoulist";
    }

    /**
     *回收已报销商品
     */
    @RequestMapping("/off_yibaoxiaoback")
        public String off_yibaoxiaoback(long offId){
        Office office = officeService.getById(offId);
        office.setIsBx(2);

        this.officeService.updateById(office);
            return "redirect:/is_bx";

    }

    /**
     * 往用户提交页面跳转
     */
    @RequestMapping("/off_adddaibaoxiao")
    public String off_adddaitijiao(){
        return "office/off_add";
    }

    /**
     *
     * 删除已报销商品的数据
     */
    @RequestMapping("/off_yibaoxiaodelete")
    public String off_yibaoxiaodelete(long offId){
        this.officeService.removeById(offId);
        return "redirect:/selectyibaoxiaoofficebyidandis_Bx";
    }


    /**
     *员工提交的商品
     */
    @RequestMapping("/off_add")
    public String offAdd(Office office, double itemPrice, int itemCount, String itemName){
        double itemTotalprice = itemPrice*itemCount;
        office.setItemName(itemName);
        office.setItemTotalprice(itemTotalprice);
        office.setPurchase(new Date());
        office.setItemCount(itemCount);
        office.setItemPrice(itemPrice);
        office.setEId(getUserId());
        office.setIsBx(0);
        this.officeService.save(office);
        return "redirect:/not_bx";
    }

    /**
     * 修改，将isBx设置为1(1代表已报销)
     */
    @RequestMapping("/off_update")
    public String offUpdate(long offId){
        Office office = officeService.getById(offId);
        double itemTotalprice =office.getItemTotalprice();


        Emp emp = empService.getById(office.getEId());
        emp.setComm(itemTotalprice + emp.getComm());

        office.setIsBx(1);
        officeService.updateById(office);
        empService.updateById(emp);
        return "redirect:/not_bx";

    }

    /**
     * 员工取消申请
     */
    @RequestMapping("/off_delete")
    public String offDelete(long offId){
       this.officeService.removeById(offId);
        return "redirect:/not_bx";
    }

}
